package com.tanhua.sso.controller;

import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Auther: you
 * @Date: 2021/05/25/15:18
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("user")
public class SmsController {
    @Autowired
    private SmsService smsService;


    @PostMapping("login")
    public ResponseEntity<ErrorResult> sendCheckCode(@RequestBody Map<String, String> map) {
        //获取请求体中传递过来的手机号
        String phone = map.get("phone");
        ErrorResult errorResult = null;

        //验证手机号是否发送验证码，同时响应发送成功与否状态
        try {
            errorResult = smsService.sendCheckCode(phone);
            if (errorResult == null) {

                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("验证码发送失败,phone=" + phone, e);
            errorResult.setErrCode("000003");
            errorResult.setErrMessage("验证码发送失败");
        }

        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);


    }
}
